import java.net.*;
import java.io.*;

public class server extends Thread {
	
	int listen_port=80;
	int trip=0;
	APServer gui2;
	
	File bb = new File("bb.html");
	
	public server(APServer in) {
		
		gui2=in;
		s("server instance created");
		this.run();
		
	}
	
	public void s(String out) {
		
		gui2.toConsole("server :: " + out);
		
	}
	
	public void run() {
		
		ServerSocket servsocket = null;
		
		s("initializing server");
		s("binding to port " + listen_port);
		
		//Bind to port		
		try {
			
			servsocket = new ServerSocket(listen_port);
			
		} catch(Exception e) {
			
			s("fatal error: " + e.getMessage());			
		}
		//We made it.. :D
		
		s("succesfully bound to port " + listen_port);
		
		//Listening loop, woo!
		while(trip != 1) {
			
			s("listening..");
			
			try {
				
				Socket connection = servsocket.accept();
				InetAddress client = connection.getInetAddress();
				
				s("! " + client.getHostName() + " [" + client.getHostAddress() + "] connected");
				
				BufferedReader input = new BufferedReader(new InputStreamReader(connection.getInputStream()));
				DataOutputStream output = new DataOutputStream(connection.getOutputStream());
				
				http_handler(input, output);
				
				
			} catch(Exception e) {
				
				s("error: " + e.getMessage());
			}
			
		}
		
		
	
	}
	
	public void http_handler(BufferedReader input, DataOutputStream output) {
		
		String path = new String();
		int method = 0;
		String tmp = new String();
		String tmp2 = new String();
		String original_page = new String();
		
		try {
			
			tmp = input.readLine();
			tmp2 = new String(tmp);
			
			tmp.toUpperCase();
			
			if(tmp.startsWith("GET")) {
				
				method=1;
				
			}
			if(tmp.startsWith("HEAD")) {
				
				method=2;
				
			}
			if(method == 0 ) {
				try {
					output.writeBytes("Error 501 - Unrecognized METHOD");
				
			
				} catch(Exception e) {
			
					s("error: " + e.getMessage());
				}
			}
			
			try {
			int s = 0;
			int e = 0;
		
			for(int i = 0; i < tmp2.length(); i++) {
			
				if(tmp2.charAt(i) == ' ' && s != 0) {
				
					e=i;
				
				}
				if(tmp2.charAt(i) == ' ' && s == 0) {
				
					s=i;
				
				}
			
			}
			
			if(tmp2.substring(s+2,e).indexOf("?") != -1) { //Client submit catch block
				
				original_page=action_handler(tmp2.substring(s+2,e));
				
			}
			
			path=tmp2.substring(s+2,e);
		
		} catch(Exception e) {
			
			s("error: " + e.getMessage());
			 
		}
		
		s("@ client requested " + new File(path).getAbsolutePath() );
		FileInputStream requestedFile = null;
		
		try {
			
			requestedFile = new FileInputStream(path);
			
		} catch(Exception e) {
			
			try {
				output.writeBytes("Error 404 :[");
			} catch (IOException e1) {
				
				e1.printStackTrace();
			}
			
		}
		
		if(method==1) {
			
			//output.writeBytes("APSUV0.5+");
			
			while(true) {
				
				int b=0;
				
				try {
					b = requestedFile.read();
				} catch (IOException e) {
					
					e.printStackTrace();
				}
				
				if(b==-1) {
					
					break;
					
				}
				try {
					output.write(b);
				} catch (IOException e) {
					
					e.printStackTrace();
				}
				
				
			}
			
		}
		
		output.close();
		requestedFile.close();
		
	} catch(Exception e) {}
		
		
	}
	
	public void shutdown() {
		
		trip=1;
		
	}

	public String action_handler(String info) {
		String page;
		
		//First, let's process the GET statement, then bring us back to the page we submitted from so we don't get a 404..
		int startCmdIndex = info.indexOf("?") + 1; //Start of the submit
		int endCmdIndex = 0;
		String Cmd = new String();
		
		for(int i = startCmdIndex; i < info.length(); i++) { //Search for the equals sign to see what we're submitting..

			if(info.charAt(i) == '=') {
				
				endCmdIndex = i;
				break;
				
			}			
		}
		
		Cmd=info.substring(startCmdIndex,endCmdIndex);
		s(Cmd);
		
		return "";
		
	}
}
